package 数组.缺失的数字;

/**
 * @author: wh(1835734390 @ qq.com)
 * @date: 2023/3/4 15:08
 * @description:剑指offer
 * @version:
 */
public class Solution {
    public static void main(String[] args) {
        int[] array = {0,1,2,3,4,5,6,7,9};
        System.out.println(missingNumber(array));
    }

    //二分查找  数组是有序数组，所以缺失的数字肯定是在其下标处
    public static int missingNumber(int[] nums) {
        int left = 0;
        int right = nums.length - 1;
        while (left <= right){
            int mid = (left + right) / 2;
            //num[mid] == mid 说明缺失的数字在右边，否则说明在左边有缺失
            if (nums[mid] == mid){
                left = mid + 1;
            }else {
                right = mid - 1;
            }
        }
        return left;
    }
}
